package com.android.questions.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.android.base.util.ActivityClickUtil;
import com.android.base.util.ToastUtil;
import com.android.questions.R;
import com.android.questions.config.AppConfig;
import com.android.questions.config.LoginConfig;
import com.android.questions.util.sound.GameMusicManager;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hx.currency.data.qa.model.QATask;
import com.liys.view.LineProView;

/**
 * FileName: TaskRewardAdapter
 * Author: admin
 * Date: 2021/11/5 10:12
 * Description:
 */
public class TaskRewardAdapter extends BaseQuickAdapter<QATask, BaseViewHolder> {

    private OnItemClickListener clickLister;

    public TaskRewardAdapter() {
        super(R.layout.item_task_reward);
        ActivityClickUtil.getInstance().clear();
    }

    @Override
    protected int getDefItemViewType(int position) {
        return position;
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, QATask qaTask) {
        Context context = getContext();
        int position = getItemPosition(qaTask);

        TextView tvTaskName = baseViewHolder.getView(R.id.tvTaskName);
        TextView tvTaskDesc = baseViewHolder.getView(R.id.tvTaskDesc);
        ImageView imgView = baseViewHolder.getView(R.id.imgView);
        TextView tvRewardNumber = baseViewHolder.getView(R.id.tvRewardNumber);
        ImageView imgTopTaskComplete = baseViewHolder.getView(R.id.imgTopTaskComplete);
        ImageView imgSzdjdh = baseViewHolder.getView(R.id.imgSzdjdh);
        ImageView imgGuangQuan = baseViewHolder.getView(R.id.imgGuangQuan);
        TextView tvProgress = baseViewHolder.getView(R.id.tvProgress);
        TextView tvWc = baseViewHolder.getView(R.id.tvWc);
        LineProView answerNumberView = baseViewHolder.getView(R.id.answerNumberView);

        imgSzdjdh.setVisibility(View.GONE);
        imgGuangQuan.setVisibility(View.GONE);
        tvTaskName.setText(qaTask.getTt());
        tvTaskDesc.setText(qaTask.getTd());

//        if (position == 0) {//新人红包
//            if (qaTask.getTs() == 2) {
//                AppConfig.setNewWithdrawal(true);
//            }
//        }

        if (qaTask.getTrt() == 2) {
            //红包奖励
            tvRewardNumber.setText("+" + qaTask.getTr() + "元");
        } else if ((qaTask.getTrt() == 6)) {
            //红包券奖励
            tvRewardNumber.setText("x " + (int) qaTask.getTr());
            imgView.setImageResource(R.drawable.icon_coin_small);
        }

        if (qaTask.getTct() == 2) {//累计正确答题
            String evenCash = "<font color='#FF3B3B'>" + (Math.min(LoginConfig.getUserAnswerSuccessNumber(), qaTask.getTcn())) + "</font>/" + qaTask.getTcn();
            tvProgress.setText(Html.fromHtml(evenCash));

            answerNumberView.setMaxProgress(qaTask.getTcn());
            answerNumberView.setProgress(LoginConfig.getUserAnswerSuccessNumber());
            if (LoginConfig.getUserAnswerSuccessNumber() >= qaTask.getTcn()) {
                if (qaTask.getTs() == 2) {
                    imgTopTaskComplete.setImageResource(R.drawable.icon_fragment_task_receive_true);
                } else {
                    imgTopTaskComplete.setBackgroundResource(R.drawable.icon_fragment_task_receive);
                    if (position == 0) {
                        imgSzdjdh.setVisibility(View.VISIBLE);
                        imgGuangQuan.setVisibility(View.VISIBLE);
                        Glide.with(context)
                                .asGif()
                                .load(R.drawable.icon_szdjdh)
                                .into(imgSzdjdh);

                        Glide.with(context)
                                .asGif()
                                .load(R.drawable.icon_guangquan)
                                .into(imgGuangQuan);
                    }
                }
            } else {
                imgTopTaskComplete.setBackgroundResource(R.drawable.icon_fragment_task_receive_false);
            }
        } else if (qaTask.getTct() == 3) {//累计登陆天数

            String evenCash = "<font color='#FF3B3B'>" + Math.min(AppConfig.getLoginDayNumber(), qaTask.getTcn()) + "</font>/" + qaTask.getTcn();
            tvProgress.setText(Html.fromHtml(evenCash));

            answerNumberView.setMaxProgress(qaTask.getTcn());
            answerNumberView.setProgress(AppConfig.getLoginDayNumber());
            if (AppConfig.getLoginDayNumber() >= qaTask.getTcn()) {
                if (qaTask.getTs() == 2) {
                    imgTopTaskComplete.setImageResource(R.drawable.icon_fragment_task_receive_true);
                } else {
                    imgTopTaskComplete.setBackgroundResource(R.drawable.icon_fragment_task_receive);
                }
            } else {
                imgTopTaskComplete.setBackgroundResource(R.drawable.icon_fragment_task_receive_false);
            }
        } else if (qaTask.getTct() == 4) {//商店好评
            tvProgress.setVisibility(View.INVISIBLE);
            answerNumberView.setVisibility(View.INVISIBLE);
            if (qaTask.getTs() == 2) {
                imgTopTaskComplete.setImageResource(R.drawable.icon_fragment_task_receive_true);
            } else {
                imgTopTaskComplete.setBackgroundResource(R.drawable.icon_fragment_task_receive_false);
            }
        }

        imgTopTaskComplete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ActivityClickUtil.getInstance().isFastClick()) {
                    //防止用户快速点击
                    return;
                }
                GameMusicManager.getInstance(context).start();
                if (clickLister != null) {
                    if (qaTask.getTs() == 2) {
                        ToastUtil.showShortToast(context, "任务已完成");
                    } else {
                        if (qaTask.getTct() == 2) {
                            if (qaTask.getTcn() == 10) {

                            }

                            if (LoginConfig.getUserAnswerSuccessNumber() >= qaTask.getTcn()) {
                                clickLister.onItemReward(position, qaTask);
                            } else {
                                ToastUtil.showShortToast(context, "去完成");
                            }
                        } else if (qaTask.getTct() == 3) {
                            if (AppConfig.getLoginDayNumber() >= qaTask.getTcn()) {

                                if (qaTask.getTcn() == 7) {

                                }
                                if (qaTask.getTcn() == 3) {

                                }

                                clickLister.onItemReward(position, qaTask);
                            } else {
                                ToastUtil.showShortToast(context, "去完成");
                            }
                        } else if (qaTask.getTct() == 4) {
                            clickLister.onItemReward(position, qaTask);
                        }
                    }
                }
            }
        });
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.clickLister = listener;
    }

    public interface OnItemClickListener {
        void onItemReward(int position, QATask qaTask);
    }
}